{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e04af759-fc46-49d9-a485-aea036caea76",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-18T18:48:07.189558Z",
     "iopub.status.busy": "2022-05-18T18:48:07.189221Z",
     "iopub.status.idle": "2022-05-18T18:48:07.669200Z",
     "shell.execute_reply": "2022-05-18T18:48:07.667952Z",
     "shell.execute_reply.started": "2022-05-18T18:48:07.189492Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "984f227e-ad07-4e24-a0fd-eb4d0c29f310",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-18T18:48:10.144677Z",
     "iopub.status.busy": "2022-05-18T18:48:10.144102Z",
     "iopub.status.idle": "2022-05-18T18:48:10.184747Z",
     "shell.execute_reply": "2022-05-18T18:48:10.183842Z",
     "shell.execute_reply.started": "2022-05-18T18:48:10.144631Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>food</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>apple</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Banana</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>oranger</td>\n",
       "      <td>3.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pineapple</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mango</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Tomato</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        food  price\n",
       "0      apple    4.0\n",
       "1     Banana    3.0\n",
       "2    oranger    3.5\n",
       "3  Pineapple    6.0\n",
       "4      mango   12.0\n",
       "5     Tomato    3.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.DataFrame(\n",
    "    {\n",
    "        \"food\": [\"apple\", \"Banana\", \"oranger\", \"Pineapple\", \"mango\", \"Tomato\"],\n",
    "        \"price\": [4, 3, 3.5, 6, 12, 3],\n",
    "    }\n",
    ")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9cc5066a-c4d0-4700-8e9d-211e57bb4432",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-18T18:48:13.158524Z",
     "iopub.status.busy": "2022-05-18T18:48:13.158156Z",
     "iopub.status.idle": "2022-05-18T18:48:13.167116Z",
     "shell.execute_reply": "2022-05-18T18:48:13.166262Z",
     "shell.execute_reply.started": "2022-05-18T18:48:13.158498Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple': 'fruit',\n",
       " 'banana': 'fruit',\n",
       " 'oranger': 'fruit',\n",
       " 'pineapple': 'fruit',\n",
       " 'mango': 'fruit',\n",
       " 'tomato': 'vegetable'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建映射-字典\n",
    "food_categories = {\n",
    "    \"apple\": \"fruit\",\n",
    "    \"banana\": \"fruit\",\n",
    "    \"oranger\": \"fruit\",\n",
    "    \"pineapple\": \"fruit\",\n",
    "    \"mango\": \"fruit\",\n",
    "    \"tomato\": \"vegetable\",\n",
    "}\n",
    "food_categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "614f0637-810c-412a-998d-4dfd94165732",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-18T18:48:28.426168Z",
     "iopub.status.busy": "2022-05-18T18:48:28.425801Z",
     "iopub.status.idle": "2022-05-18T18:48:28.437949Z",
     "shell.execute_reply": "2022-05-18T18:48:28.437341Z",
     "shell.execute_reply.started": "2022-05-18T18:48:28.426130Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>food</th>\n",
       "      <th>price</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>apple</td>\n",
       "      <td>4.0</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Banana</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>oranger</td>\n",
       "      <td>3.5</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pineapple</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mango</td>\n",
       "      <td>12.0</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Tomato</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        food  price category\n",
       "0      apple    4.0    fruit\n",
       "1     Banana    3.0      NaN\n",
       "2    oranger    3.5    fruit\n",
       "3  Pineapple    6.0      NaN\n",
       "4      mango   12.0    fruit\n",
       "5     Tomato    3.0      NaN"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用映射完成数据转换\n",
    "data[\"category\"] = data[\"food\"].map(food_categories)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f44170a-dd7e-4516-a1e5-093bdf0c24cc",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## 一、通过映射完成数据转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "83b22a74-7b91-41a6-ad78-346f15c3bb99",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-18T18:53:52.965456Z",
     "iopub.status.busy": "2022-05-18T18:53:52.965199Z",
     "iopub.status.idle": "2022-05-18T18:53:52.975028Z",
     "shell.execute_reply": "2022-05-18T18:53:52.974508Z",
     "shell.execute_reply.started": "2022-05-18T18:53:52.965441Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>food</th>\n",
       "      <th>price</th>\n",
       "      <th>category</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>apple</td>\n",
       "      <td>4.0</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Banana</td>\n",
       "      <td>3.0</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>oranger</td>\n",
       "      <td>3.5</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pineapple</td>\n",
       "      <td>6.0</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mango</td>\n",
       "      <td>12.0</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Tomato</td>\n",
       "      <td>3.0</td>\n",
       "      <td>vegetable</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        food  price   category\n",
       "0      apple    4.0      fruit\n",
       "1     Banana    3.0      fruit\n",
       "2    oranger    3.5      fruit\n",
       "3  Pineapple    6.0      fruit\n",
       "4      mango   12.0      fruit\n",
       "5     Tomato    3.0  vegetable"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 利用映射完成数据转换，设置大小写转换\n",
    "data[\"category\"] = data[\"food\"].str.lower().map(food_categories)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "172d10c6-78c0-4533-9a4f-464725d67449",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## 二、通过函数完成数据转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "afcd2f99-3698-4f6f-917a-4020dcfe7a6a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-18T19:08:07.948719Z",
     "iopub.status.busy": "2022-05-18T19:08:07.948267Z",
     "iopub.status.idle": "2022-05-18T19:08:07.961296Z",
     "shell.execute_reply": "2022-05-18T19:08:07.960275Z",
     "shell.execute_reply.started": "2022-05-18T19:08:07.948687Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>food</th>\n",
       "      <th>price</th>\n",
       "      <th>category</th>\n",
       "      <th>category1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>apple</td>\n",
       "      <td>4.0</td>\n",
       "      <td>fruit</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Banana</td>\n",
       "      <td>3.0</td>\n",
       "      <td>fruit</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>oranger</td>\n",
       "      <td>3.5</td>\n",
       "      <td>fruit</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Pineapple</td>\n",
       "      <td>6.0</td>\n",
       "      <td>fruit</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mango</td>\n",
       "      <td>12.0</td>\n",
       "      <td>fruit</td>\n",
       "      <td>fruit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Tomato</td>\n",
       "      <td>3.0</td>\n",
       "      <td>vegetable</td>\n",
       "      <td>vegetable</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        food  price   category  category1\n",
       "0      apple    4.0      fruit      fruit\n",
       "1     Banana    3.0      fruit      fruit\n",
       "2    oranger    3.5      fruit      fruit\n",
       "3  Pineapple    6.0      fruit      fruit\n",
       "4      mango   12.0      fruit      fruit\n",
       "5     Tomato    3.0  vegetable  vegetable"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用函数的形式来实现类似的数据转换\n",
    "data[\"category1\"] = data[\"food\"].map(lambda x: food_categories[x.lower()])\n",
    "data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
